package de.tum.in.msspp.message;


import java.util.List;

import de.tum.in.msspp.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class MessageListAdapter extends ArrayAdapter<Message> {
	
	private List<Message> messageList;

	public MessageListAdapter(Context context, int textViewResourceId,
			List<Message> objects) {
		super(context, textViewResourceId, objects);
		messageList = objects;
	}
	
	/**
	 * Checks if the given message already exists in our list
	 * @param m - the message to check
	 * @return
	 */
	public Boolean contains(Message m){
		if(messageList.contains(m))
			return true;
		else
			return false;
	}
	
	/** Custom getView() method for display of Messages in ListView */
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if(convertView == null)
		{
			LayoutInflater vi = (LayoutInflater)super.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.messagerow, null);
		}
		
		Message message = super.getItem(position);
		if(message != null){
			TextView tSender = (TextView)convertView.findViewById(R.id.messageSender);
			TextView tData = (TextView)convertView.findViewById(R.id.messageData);
			if(tSender != null){
				tSender.setText(message.getSender());
			}
			if(tData != null){
				tData.setText(message.getData());
			}
 		}
 		
		return convertView;
	}
}

